/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.app.tabsresource.rest;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.app.ad.service.AduserTabsDetailService;
import me.zhengjie.modules.app.ad.service.AduserXHotelService;
import me.zhengjie.modules.app.ad.service.AduserXTabsindexService;
import me.zhengjie.modules.app.ad.service.dto.AduserTabsDetailDto;
import me.zhengjie.modules.app.ad.service.dto.AduserTabsDetailQueryCriteria;
import me.zhengjie.modules.app.ad.service.dto.AduserXHotelDto;
import me.zhengjie.modules.app.ad.service.dto.AduserXHotelQueryCriteria;
import me.zhengjie.modules.app.ad.service.dto.AduserXTabsindexDto;
import me.zhengjie.modules.app.ad.service.dto.AduserXTabsindexQueryCriteria;
import me.zhengjie.modules.app.tabs.service.TabsInfoService;
import me.zhengjie.modules.app.tabs.service.dto.TabsInfoDto;
import me.zhengjie.modules.app.tabsresource.domain.TabsResourceInfo;
import me.zhengjie.modules.app.tabsresource.domain.dto.TabsResourceInfoEditDto;
import me.zhengjie.modules.app.tabsresource.service.TabsResourceInfoService;
import me.zhengjie.modules.app.tabsresource.service.dto.TabsResourceInfoDto;
import me.zhengjie.modules.app.tabsresource.service.dto.TabsResourceInfoQueryCriteria;

import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONArray;

import io.swagger.annotations.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author Haojie Li
* @date 2021-03-01
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "应用：选项卡资源展示信息")
@RequestMapping("/api/tabsResourceInfo")
public class TabsResourceInfoController {

	private final TabsInfoService tabsInfoService;

    private final TabsResourceInfoService tabsResourceInfoService;

    private final AduserXHotelService aduserXHotelService;
    private final AduserTabsDetailService aduserTabsDetailService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('tabsResourceInfo:list')")
    public void download(HttpServletResponse response, TabsResourceInfoQueryCriteria criteria) throws IOException {
        tabsResourceInfoService.download(tabsResourceInfoService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询选项卡资源展示信息")
    @ApiOperation("查询选项卡资源展示信息")
    @PreAuthorize("@el.check('tabsResourceInfo:list')")
    public ResponseEntity<Object> query(TabsResourceInfoQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(tabsResourceInfoService.queryAll(criteria,pageable),HttpStatus.OK);
    }


    @GetMapping("findByHotelId")
    @Log("根据酒店信息查询选项卡资源展示信息")
    @ApiOperation("根据酒店信息查询选项卡资源展示信息")
    @PreAuthorize("@el.check('tabsResourceInfo:list')")
    public ResponseEntity<Object> findByHotelId(TabsResourceInfoQueryCriteria criteria, Pageable pageable){

    	List<TabsResourceInfoDto> tabsResourceInfoDtos = tabsResourceInfoService.queryAll(criteria);

    	//查询tabsinfo 相关信息
    	TabsInfoDto tabsInfoDto = tabsInfoService.findById(criteria.getTabsId());

    	//查询酒店是否分配广告商，如果没有分配直接返回查询出来的数据，如果分配了，需要将广告商的广告数据进行组装
    	AduserXHotelQueryCriteria aduserXHotelQueryCriteria = new AduserXHotelQueryCriteria();
    	aduserXHotelQueryCriteria.setHotelId(criteria.getHotelId());
    	List<AduserXHotelDto> aduserXHotelDtos = aduserXHotelService.queryAll(aduserXHotelQueryCriteria);
    	if(aduserXHotelDtos != null && aduserXHotelDtos.size() > 0) {
    		//拼接数据,查询广告商配置的坑位广告信息
    		AduserTabsDetailQueryCriteria aduserTabsDetailQueryCriteria = new AduserTabsDetailQueryCriteria();
    		aduserTabsDetailQueryCriteria.setAdUserId(aduserXHotelDtos.get(0).getUserId());
    		aduserTabsDetailQueryCriteria.setTabIndex(tabsInfoDto.getTabSeq());

    		List<AduserTabsDetailDto> aduserTabsDetailDtos = aduserTabsDetailService.queryAll(aduserTabsDetailQueryCriteria);
    		//如果查询出来有匹配数据，就替换酒店自己设定的数据，而且只会有一条

    		if(aduserTabsDetailDtos != null && aduserTabsDetailDtos.size() == 1) {
    			List<TabsResourceInfoDto> tabsResourceInfoDtosNew = new ArrayList<TabsResourceInfoDto>();
    			AduserTabsDetailDto aduserTabsDetailDto = aduserTabsDetailDtos.get(0);
				TabsResourceInfoDto tabsResourceInfoDto = tabsResourceInfoDtos.get(0);
				TabsResourceInfoDto temp = new TabsResourceInfoDto();
//    			if(aduserTabsDetailDto.getApkParams() != null) {
//    				BeanUtils.copyProperties(tabsResourceInfoDto, temp);
//    				temp.setResourceType("apk");
//    				temp.setResourceUrl(aduserTabsDetailDto.getApkParams());
//    				tabsResourceInfoDtosNew.add(temp);
//    			}
				if(aduserTabsDetailDto.getLeftMv() != null) {//视频
					TabsResourceInfoDto temp2 = new TabsResourceInfoDto();
					BeanUtils.copyProperties(tabsResourceInfoDto, temp2);
					temp2.setResourceType("mv");
					temp2.setResourceUrl(aduserTabsDetailDto.getLeftMv());
					temp2.setResourceDisplayPosition("left");
    				tabsResourceInfoDtosNew.add(temp2);
    			}
				if(aduserTabsDetailDto.getRightPics() != null) {
    				String arrayStr = aduserTabsDetailDto.getRightPics();
    				JSONArray jsonArray = JSONArray.parseArray(arrayStr);
					TabsResourceInfoDto temp3 = new TabsResourceInfoDto();
    				for(int index = 0; index < jsonArray.size(); index ++) {
    					temp3 = new TabsResourceInfoDto();
						BeanUtils.copyProperties(tabsResourceInfoDto, temp3);
						temp3.setResourceType("img");
						temp3.setResourceUrl(jsonArray.getJSONObject(index).getString("url"));
						temp3.setResourceDisplayPosition("right");
        				tabsResourceInfoDtosNew.add(temp3);
    				}
    			}
				tabsResourceInfoDtos.clear();
				tabsResourceInfoDtos.addAll(tabsResourceInfoDtosNew);
    		}
    	}

        return new ResponseEntity<>(tabsResourceInfoDtos,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增选项卡资源展示信息")
    @ApiOperation("新增选项卡资源展示信息")
    @PreAuthorize("@el.check('tabsResourceInfo:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody TabsResourceInfo resources){
        return new ResponseEntity<>(tabsResourceInfoService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改选项卡资源展示信息")
    @ApiOperation("修改选项卡资源展示信息")
    @PreAuthorize("@el.check('tabsResourceInfo:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody TabsResourceInfo resources){
        tabsResourceInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }




    @Log("删除选项卡资源展示信息")
    @ApiOperation("删除选项卡资源展示信息")
    @PreAuthorize("@el.check('tabsResourceInfo:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody String[] ids) {
        tabsResourceInfoService.deleteAll(ids);

        return new ResponseEntity<>(HttpStatus.OK);
    }



    @PutMapping("/x")
    @Log("修改选项卡资源展示信息")
    @ApiOperation("修改选项卡资源展示信息")
    public ResponseEntity<Object> updateX(@RequestBody TabsResourceInfoEditDto tabsResourceInfoEditDto){

        tabsResourceInfoService.updateX(tabsResourceInfoEditDto);

        System.out.println(JSON.toJSONString(tabsResourceInfoEditDto));

        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

}
